﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample63
{
    class Program
    {
        static async Task Main(string[] args)
        {
            CreateISEA(true);
            var h = Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) config.AddVulkanJsonFile("Vulkan.json");
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) config.AddVulkanJsonFile("Vulkan-Linux.json");
                    else throw new NotSupportedException("Not supported Platform");
                })
                .ConfigureSgrAHost((sgra) =>
                {
                    sgra.AddVulkanEnvironmental();
                    sgra.UseStartup<Startup>();
                }).ConfigureLogging((logger) => logger.AddConsole()); ;
            await h.RunConsoleAsync();
        }


        static void CreateISEA(bool isNeed)
        {
            var texPath = Path.Combine(AppContext.BaseDirectory, "Assets", "Textures");
            var imageJson = JsonDocument.Parse(File.ReadAllText(Path.Combine(texPath, "robot.json")));
            var robotJson = imageJson.RootElement.GetProperty("Robot").GetRawText();
            var samplerJson0 = imageJson.RootElement.GetProperty("Sampler0").GetRawText();
            var samplerJson1 = imageJson.RootElement.GetProperty("Sampler1").GetRawText();
            var samplerJson2 = imageJson.RootElement.GetProperty("Sampler2").GetRawText();
            var samplerJson3 = imageJson.RootElement.GetProperty("Sampler3").GetRawText();
            ISEADocument document = new ISEADocument();
            ConfigNode robotConfig = document.CreateConfigNode("robot", robotJson, ConfigEngine.Vulkan);
            ConfigNode sampler0 = document.CreateConfigNode("Sampler0", samplerJson0, ConfigEngine.Vulkan);
            ConfigNode sampler1 = document.CreateConfigNode("Sampler1", samplerJson1, ConfigEngine.Vulkan);
            ConfigNode sampler2 = document.CreateConfigNode("Sampler2", samplerJson2, ConfigEngine.Vulkan);
            ConfigNode sampler3 = document.CreateConfigNode("Sampler3", samplerJson3, ConfigEngine.Vulkan);
            IOP.ISEA.ImageNode image = document.CreateImageNode("RobotImage", Path.Combine(texPath, "robot0.bntex"));
            List<float[]> vDatas = new List<float[]>(3);
            vDatas.Add(new float[]
            {
                9,9,0,4,0, -9,9,0,0,0, -9,-9,0,0,4,
                9,9,0,4,0, -9,-9,0,0,4, 9,-9,0,4,4
            });
            vDatas.Add(new float[]
            {
                9,9,0,4,0, -9,9,0,0,0, -9,-9,0,0,2,
                9,9,0,4,0, -9,-9,0,0,2, 9,-9,0,4,2
            });
            vDatas.Add(new float[]
            {
                9,9,0,1,0, -9,9,0,0,0, -9,-9,0,0,1,
                9,9,0,1,0, -9,-9,0,0,1, 9,-9,0,1,1
            });
            for(int i = 0; i < 3; i++)
            {
                MeshNode mesh = document.CreateEmptyMeshNode($"Robot{i}Mesh", 6)
                    .AddMeshData(vDatas[i], 20, 0, MeshDataPurpose.COMPLEX, false);
                TextureNode tex0 = document.CreateTextureNode($"Robot{i}Tex0", TextureUsage.CombinedImageSampler, image,
                    robotConfig, sampler0, 1);
                TextureNode tex1 = document.CreateTextureNode($"Robot{i}Tex1", TextureUsage.CombinedImageSampler, image,
                    robotConfig, sampler1, 1);
                TextureNode tex2 = document.CreateTextureNode($"Robot{i}Tex2", TextureUsage.CombinedImageSampler, image,
                    robotConfig, sampler2, 1);
                TextureNode tex3 = document.CreateTextureNode($"Robot{i}Tex3", TextureUsage.CombinedImageSampler, image,
                    robotConfig, sampler3, 1);
                document.CreateRenderObjectNode($"Robot{i}", mesh, tex0, tex1, tex2, tex3);
            }
            document.Save(AppContext.BaseDirectory, "Test63");
        }
    }
}
